package com.yanqu.road.server.cmd.activity.orewar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.activity.eActivityOreWarType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.logic.rank.RankListModel;
import com.yanqu.road.pb.activity.CrossActivityRankProto;
import com.yanqu.road.pb.orewaractivity.OreWarActivityProto;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.pb.rank.YRankProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.orewar.OreWarActivityMgr;
import com.yanqu.road.server.manger.activity.orewar.OreWarActivityRankMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_ORE_WAR_ACTIVITY_RANK, desc = "个人盐场活动排行榜")
public class OreWarActivityRankListCmd extends AbstractCommand {

    private int activityId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        OreWarActivityProto.OreWarActivityRankListReqMsg reqMsg = OreWarActivityProto.OreWarActivityRankListReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

    }

    @Override
    public void sendData(GamePlayer player) {
        OreWarActivityProto.OreWarActivityRankListRespMsg.Builder respMsg = OreWarActivityProto.OreWarActivityRankListRespMsg.newBuilder();
        respMsg.setMyRank(-1);
        ActivityInfo openActivityInfo = OreWarActivityMgr.getOpenActivityInfo(activityId);
        if (null != openActivityInfo) {
            if (openActivityInfo.getChildType() == eActivityOreWarType.OreWar.getChildType()) {
                RankListModel rankListModel = OreWarActivityRankMgr.getRankListModel(openActivityInfo.getActivityId());
                if (null != rankListModel) {
                    respMsg.setRankList(rankListModel.getRankListMsg());
                    respMsg.setMyRank(rankListModel.unlockGetMyRank(player.getUserId()));
                }
            } else if (openActivityInfo.getChildType() == eActivityOreWarType.CrossOreWar.getChildType()) {
             /*   RankProto.RankListMsg.Builder rankListMsg = CrossDataMgr.getCrossRankActivityRankMsg(activityId);
                int maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
                int conditionType = OreWarActivityMgr.CONDITION_TYPE_USER;
                maxRank = OreWarActivityMgr.getMaxRankByType(activityId, conditionType);
                while (null != rankListMsg.getRankListList() && rankListMsg.getRankListCount() > maxRank){
                    rankListMsg.removeRankList(rankListMsg.getRankListCount() -1 );
                }
                respMsg.setRankList(rankListMsg);
                respMsg.setMyRank(ActivityMgr.getMyCrossRank(player.getUserId(), rankListMsg));*/
//                CrossActivityRankProto.CrossActivityRankReqMsg.Builder msg = CrossActivityRankProto.CrossActivityRankReqMsg.newBuilder();
//                msg.setActivityId(activityId);
//                msg.setBigRankType(0);
//                player.sendPacket(Protocol.C2_CROSS_GET_ORE_ACTIVITY_RANK, msg);
//                return;

                int groupId = GameUnionActivityGroupMgr.getGroupId(activityId, player.getUserId());
                if (groupId == -1) {
                    respMsg.setRankList(RankProto.RankListMsg.newBuilder());
                    respMsg.setMyRank(-1);
                }

                YRankProto.YRankReqMsg.Builder reqMsg = YRankProto.YRankReqMsg.newBuilder();
                reqMsg.setActivityId(activityId);
                reqMsg.setGroupId(groupId);
                reqMsg.setIdKey(String.valueOf(OreWarActivityMgr.CONDITION_TYPE_USER));
                player.sendUnionActivityGroupPacket(CrossProtocol.C2_CROSS_YRANK_GET_USER_RANK_LIST, reqMsg, activityId);

                return;
            }
            player.sendPacket(Protocol.U_ORE_WAR_ACTIVITY_RANK, respMsg);
        }
    }
}


